package archimede;

import java.awt.Font;
import java.awt.Graphics;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

import net.miginfocom.swing.MigLayout;

public class PannelloPiantina extends JPanel {

	private int tab;
	private Piantina tabbed;
	private StartingWindow st;
	private BarraPulsanti barra;

	protected void paintComponent(Graphics g) {
		setOpaque(false);
		g.drawImage((new ImageIcon(ClassLoader.getSystemResource("sfondo.jpg"))).getImage(), 0, 0, null);
		super.paintComponent(g);
	}

	public PannelloPiantina(Piantina tabbed, int count, StartingWindow st) {
		super();
		this.st = st;
		this.tabbed = tabbed;
		tab = count;
		setLayout(new MigLayout());
		barra = new BarraPulsanti(this, st);
		add(barra, "dock north");
	}

	public void refresh() {
		paintAll(getGraphics());
	}

	public void setTitle(String title) {
		tabbed.setTitleAt(tab, title);
	}

	public void salva() {
		// System.out.println("tab "+tab);
		Salva s = new Salva(barra.getpanel(), st.getCount());
		// System.out.println("dir "+st.getCount());
		s.salvaImmaginePNG("piantina" + tab + ".png");
	}

	public String getTitle() {
		return tabbed.getTitleAt(tab);
	}

	public void setPiantina(String path) {
		Properties pc = System.getProperties();
		String homePath = pc.getProperty("user.home");
		String directoryProject = homePath + pc.getProperty("file.separator") + "ArchimedeSuper" + pc.getProperty("file.separator") + st.getCount()
				+ pc.getProperty("file.separator");

		barra.setElement(directoryProject + "piantina" + tab + ".png");
	}

	public Document pdf(Document doc) {
		doc.newPage();
		Chunk underline = new Chunk("5. Pianta dell'edificio " + getTitle(), FontFactory.getFont(FontFactory.TIMES, 15, Font.BOLD));
		underline.setUnderline(1.2f, -2f); // 0.1 thick, -2 y-location

		try {
			doc.add(underline);
		} catch (DocumentException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		Properties pc = System.getProperties();
		String homePath = pc.getProperty("user.home");
		String directoryProject = homePath + pc.getProperty("file.separator") + "ArchimedeSuper" + pc.getProperty("file.separator") + st.getCount()
				+ pc.getProperty("file.separator") + "piantina" + tab + ".png";

		PdfPTable table = new PdfPTable(4);
		table.setWidthPercentage(100);
		try {
			com.itextpdf.text.Image img = com.itextpdf.text.Image.getInstance(directoryProject);

			float w = img.getWidth();
			float h = img.getHeight();
			float propozione = h / w;
			Rectangle page = doc.getPageSize();

			// System.out.println("page h" + page.getHeight() + " w"
			// + page.getWidth());
			// System.out.println("img h" + h + " w" + w);
			if (w > page.getWidth()-100 && h > page.getHeight()-250)
				img.scaleAbsolute(page.getWidth() - 100, page.getHeight() - 250);
			else if (w > page.getWidth() -100&& h < page.getHeight()-250)
				img.scaleAbsolute(page.getWidth() - 100, h);
			else if (w < page.getWidth()-100 && h > page.getHeight()-250)
				img.scaleAbsolute(w, page.getHeight() - 250);
			PdfPCell cos = new PdfPCell(img);
			cos.setHorizontalAlignment(Element.ALIGN_CENTER);
			cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
			cos.setBorder(PdfPCell.NO_BORDER);
			cos.setColspan(4);
			table.addCell(cos);

			cos = new PdfPCell(new Phrase("Legenda piantina", FontFactory.getFont(FontFactory.TIMES, 12, Font.BOLD)));
			cos.setHorizontalAlignment(Element.ALIGN_LEFT);
			cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
			cos.setBorder(PdfPCell.NO_BORDER);
			cos.setColspan(4);
			table.addCell(cos);

			cos = new PdfPCell(com.itextpdf.text.Image.getInstance(ClassLoader.getSystemResource("pright_legenda.jpg")));
			cos.setHorizontalAlignment(Element.ALIGN_CENTER);
			cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
			cos.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cos);

			cos = new PdfPCell(new Phrase("punto rilievo critico", FontFactory.getFont(FontFactory.TIMES, 10)));
			cos.setHorizontalAlignment(Element.ALIGN_CENTER);
			cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
			cos.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cos);

			cos = new PdfPCell(com.itextpdf.text.Image.getInstance(ClassLoader.getSystemResource("tright_legenda.jpg")));
			cos.setHorizontalAlignment(Element.ALIGN_CENTER);
			cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
			cos.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cos);

			cos = new PdfPCell(new Phrase("punto misurazione potenziali elettrici", FontFactory.getFont(FontFactory.TIMES, 10)));
			cos.setHorizontalAlignment(Element.ALIGN_CENTER);
			cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
			cos.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cos);

			cos = new PdfPCell(com.itextpdf.text.Image.getInstance(ClassLoader.getSystemResource("clima_legenda.jpg")));
			cos.setHorizontalAlignment(Element.ALIGN_CENTER);
			cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
			cos.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cos);

			cos = new PdfPCell(new Phrase("punto rilievo clima", FontFactory.getFont(FontFactory.TIMES, 10)));
			cos.setHorizontalAlignment(Element.ALIGN_CENTER);
			cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
			cos.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cos);

			cos = new PdfPCell(com.itextpdf.text.Image.getInstance(ClassLoader.getSystemResource("mright_legenda.jpg")));
			cos.setHorizontalAlignment(Element.ALIGN_CENTER);
			cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
			cos.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cos);

			cos = new PdfPCell(new Phrase("Prelievo campione misura ponderale", FontFactory.getFont(FontFactory.TIMES, 10)));
			cos.setHorizontalAlignment(Element.ALIGN_CENTER);
			cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
			cos.setBorder(PdfPCell.NO_BORDER);
			cos.setColspan(4);
			table.addCell(cos);
			doc.add(table);
		} catch (BadElementException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return doc;
	}

	public String print() {
		return tabbed.getTitleAt(tab);
	}

	public int getTab() {
		return tab;
	}

}
